<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/2/22
 * Time: 15:36
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Banner extends Base
{
    /**
     * 轮播图首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $banner_name = input('banner_name');
            $where['banner_status'] = ['BETWEEN', [1, 2]];

            if($banner_name) $where['banner_name'] = ['LIKE', '%' . $banner_name . '%'];

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_banner')->where($where)->count();// 获取总条数
            $lists = Db::table('up_banner')
                ->where($where)
                ->field('banner_id, banner_name, banner_image, banner_status, banner_sort, banner_time')
                ->page($Nowpage, $limits)
                ->order('banner_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        return $this->fetch();
    }

    /**
     * 新增轮播
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            $banner_name = input('banner_name');
            $banner_image = input('banner_image');
            $banner_detail_en = input('banner_detail_en');
            $banner_detail_es = input('banner_detail_es');
            $banner_detail_tp = input('banner_detail_tp');
            $banner_status = input('banner_status');
            $banner_sort = input('banner_sort');

            $insert = [
                'banner_name' => $banner_name,
                'banner_image' => $banner_image,
                'banner_detail_en' => $banner_detail_en,
                'banner_detail_es' => $banner_detail_es,
                'banner_detail_tp' => $banner_detail_tp,
                'banner_status' => $banner_status,
                'banner_sort' => $banner_sort,
                'banner_time' => initTime()
            ];
            $res = Db::table('up_banner')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑轮播
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function bannerEdit(){
        $banner_id = input('banner_id');
        if(Request::instance()->isPost()){
            $banner_name = input('banner_name');
            $banner_image = input('banner_image');
            $banner_detail_en = input('banner_detail_en');
            $banner_detail_es = input('banner_detail_es');
            $banner_detail_tp = input('banner_detail_tp');
            $banner_status = input('banner_status');
            $banner_sort = input('banner_sort');

            $update = [
                'banner_name' => $banner_name,
                'banner_image' => $banner_image,
                'banner_detail_en' => $banner_detail_en,
                'banner_detail_es' => $banner_detail_es,
                'banner_detail_tp' => $banner_detail_tp,
                'banner_status' => $banner_status,
                'banner_sort' => $banner_sort,
                'banner_update_time' => initTime()
            ];
            $res = Db::table('up_banner')->where('banner_id', $banner_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $banner = Db::table('up_banner')->where('banner_id', $banner_id)->field('banner_time, banner_update_time', true)->find();
        return $this->fetch('', ['banner_id' => $banner_id, 'data' => $banner]);
    }

    /**
     * 轮播详情
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bannerDetail(){
        $banner_id = input('banner_id');
        $detail = Db::table('up_banner')
            ->where('banner_id', $banner_id)
            ->field('banner_id, banner_time, banner_update_time', true)
            ->find();
        return $this->fetch('', ['data' => $detail]);
    }

    /**
     * 上架/下架
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function changeStatus(){
        $banner_id = input('id');
        $status = Db::table('up_banner')->where('banner_id', $banner_id)->field('banner_status')->find();
        $newStatus = $status['banner_status'] == 1 ? 2 : 1;

        $res = Db::table('up_banner')->where('banner_id', $banner_id)->setField('banner_status', $newStatus);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 删除轮播
     * @return \think\response\Json
     */
    public function bannerDel(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->setField('banner_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}